package de.gameduell.framework.application {
	import de.gameduell.framework.application.Game;
	import de.gameduell.framework.application.controller.SingleplayerController;
	import de.gameduell.framework.asset.AssetGroup;
	import de.gameduell.framework.asset.IAsset;
	import de.gameduell.framework.debug.Debug;
	import de.gameduell.framework.game.GameSet.ISet;
	import de.gameduell.framework.game.GameSet.SetEvent;
	import de.gameduell.framework.gui.popup.Popup;
	import de.gameduell.framework.locale.Locale;
	import de.gameduell.net.src.communication.CommunicationEvent;
	
	import flash.display.Stage;
	import flash.events.Event;	

	/**
	 * @author cbra
	 */
	public class SingleplayerGame extends Game {
		
		private static const TUTORIAL:String = "tutorial";
		private static const TUTORIAL_LOCALE:String = "tutorial_locale";
		private static const ERROR_POPUP_TIME:int = 9999999;
		
		protected var _controller:SingleplayerController;
		private var _gameSet:ISet;
		private var tutorial:ITutorial = null;
		
		/**  
		 * @param aController = the SinglePlayerController of the current application 
		**/	
		protected function set controller(aController:SingleplayerController):void {
			_controller = aController;
			initController(aController);
		}
		
		/**  
		 * @param aStage    = the stage of the application 
		 * @param aAssets   = comprises the assets from the configfile
		 * @param aSettings = comprises the settings from the configfile
		 * @param isOnline  = is the application online or standalone
		**/	
		override public function init(aStage:Stage, aAssets:AssetGroup, aSettings:Settings,isOnline:Boolean):void {
			var tutorialXmlAsset:IAsset = aAssets.getAssetById(TUTORIAL_LOCALE);
			if(tutorialXmlAsset != null) {
				var tutorialXml:XML = XML(tutorialXmlAsset.data);	
				locale = Locale.getInstance();	
				locale.addXML(tutorialXml);
			}

			super.init(aStage, aAssets, aSettings, isOnline);	

			var tutApp:IAsset = aAssets.getAssetById(TUTORIAL);
			if(tutApp != null) {
				tutorial = ITutorial(tutApp.data);
				tutorial.initButtons(aAssets, aSettings);
				tutorial.addEventListener(Tutorial.TUTORIAL_FINISHED, onTutorialFinished);
				window.getContent().addChild(tutorial.clip);
				removePreloaderScreen();
			}else{
				start();
			}
		}
		
		/**  
		 * after the tutorial is finished remove the tutorial
		**/	
		private function onTutorialFinished(event:Event):void {
			window.getContent().removeChild(tutorial.clip);
			start();
		}
		
		/**  
		 * @param aSet = the gameset
		 * sets the gameset and creates a listener for the "BUILDED" event
		**/	
		public function set gameSet(aSet:ISet):void{
			_gameSet = aSet;
			_gameSet.addEventListener(SetEvent.BUILDED, setBuilded);
			_controller.setGameSet(aSet);
			if(isStandalone) {
				Debug.trace("SinglePlayerGame::set gameSet: create random set ...", Debug.DEFAULT);
				_gameSet.createRandomSet();
			}
		}
		
		/**  
		 * shows the disconnection popup if the connection failed
		**/
		override protected function onDisconnected(event:CommunicationEvent):void {
			Debug.trace("Game::onDisconnected", Debug.ERROR);
			var connectionLostPopup:Popup = new Popup("errorPopup","connectionLostPopup",ERROR_POPUP_TIME, true);
			connectionLostPopup.modal = true;
			connectionLostPopup.registerButton("send", _controller.exit);
			connectionLostPopup.addEventListener(Popup.HIDE, _controller.exit);
			connectionLostPopup.show();
		}
		
		/**  
		 * removes the preloaderscreen after the set has been built
		**/
		protected function setBuilded(event:Event = null):void {
			if(tutorial == null){
				removePreloaderScreen();
			}
		}
		
		/**  
		 * starts the game
		**/
		public function start():void {
			
		}
	}
}
